package com.dwy2002.room.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dwy2002.common.Result;
import com.dwy2002.common.exception.EmptyIdException;
import com.dwy2002.common.utils.OssUtils;
import com.dwy2002.enums.Status;
import com.dwy2002.pojo.dto.rooms.RoomImagesDTO;
import com.dwy2002.pojo.po.rooms.RoomImages;
import com.dwy2002.pojo.vo.UploadImagesVO;
import com.dwy2002.pojo.vo.rooms.RoomImageVO;
import com.dwy2002.room.mapper.RoomImagesMapper;
import com.dwy2002.room.service.IRoomImagesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

import static com.dwy2002.common.Result.fail;
import static com.dwy2002.common.Result.success;
import static com.dwy2002.common.utils.OssUtils.isFileOver;
import static com.dwy2002.common.utils.Utils.*;
import static com.dwy2002.enums.OssEnum.ROOM_IMAGE;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/03/01
 */
@Service
@RequiredArgsConstructor
public class RoomImagesServiceImpl extends ServiceImpl<RoomImagesMapper, RoomImages> implements IRoomImagesService {
    private final OssUtils ossUtils;

    /**
     * 保存商品图片
     *
     * @param image 包间图片接收类
     * @return true: 插入成功 false:插入失败
     * @author 杜伟毅
     * @since 2025/3/1 0001 0:44
     */
    @Override
    public Boolean saveRoomImage(RoomImagesDTO image) {
        if (isEmptyOrNull(image.getRoomId()) || isEmptyOrNull(image.getImageUrl())) return false;

        LambdaQueryWrapper<RoomImages> lqw = new LambdaQueryWrapper<>();
        lqw.eq(RoomImages::getRoomId, image.getRoomId())
                .eq(RoomImages::getImageUrl, image.getImageUrl());
        //防止相同的图片重新保存
        if (exists(lqw)) {
            return false;
        }
        return save(BeanUtil.copyProperties(image, RoomImages.class));
    }

    /**
     * 删除包间图片
     *
     * @param roomIds 包间id集合
     * @author 杜伟毅
     * @since 2025/3/2 0002 15:28
     */
    @Override
    public void deleteBathImage(List<String> roomIds) {
        if (isListEmpty(roomIds)) return;
        LambdaQueryWrapper<RoomImages> lqw = new LambdaQueryWrapper<>();
        lqw.in(RoomImages::getRoomId, roomIds);
        remove(lqw);
    }

    /**
     * 根据图片id删除
     *
     * @param imgId 图片id
     * @author 杜伟毅
     * @since 2025/5/2 0002 15:12
     */
    @Override
    public Result<Void> deleteImage(String imgId) {
        if (StringUtils.isEmpty(imgId)) throw new EmptyIdException("deleteImage");
        RoomImages image = getById(imgId);
        if (ObjectUtils.isEmpty(image)) return Result.of(Status.BAD_REQUEST, "图片不存在", null);
        removeById(imgId);
        ossUtils.deleteImage(image.getImageUrl());
        return Result.of(Status.NO_CONTENT, "图片删除成功", null);
    }

    /**
     * 获取包间图片集合
     *
     * @param id 包间
     * @author 杜伟毅
     * @since 2025/5/5 0005 13:40
     */
    @Override
    public List<RoomImageVO> getRoomImages(String id) {
        if (StringUtils.isEmpty(id)) throw new EmptyIdException("getRoomImages");
        LambdaQueryWrapper<RoomImages> lqw = new LambdaQueryWrapper<>();
        lqw.eq(RoomImages::getRoomId, id);
        return BeanUtil.copyToList(list(lqw), RoomImageVO.class);
    }

    /**
     * 用于上传包间图片
     *
     * @param images 图片列表文件
     * @author 杜伟毅
     * @since 2025/1/26 0026 16:25
     */
    @Override
    public Result<List<UploadImagesVO>> uploadRoomsImages(List<MultipartFile> images) throws IOException {
        // 1. 检查列表是否为空
        if (isListEmpty(images)) {
            return fail(Status.BAD_REQUEST, "图片列表为空");
        }

        // 2. 检查文件数量是否超过限制（最多5张图片）
        if (images.size() > 5) {
            return fail(Status.BAD_REQUEST, "最多只能上传5张图片");
        }

        // 3. 检查列表中每个文件是否为空
        for (MultipartFile file : images) {
            if (isFileEmpty(file)) {
                return fail(Status.BAD_REQUEST, "图片文件为空");
            }
            //检查文件大小
            if (isFileOver(file.getSize(), 20.1)) { // 20MB
                return fail(Status.BAD_REQUEST, "图片超出范围，请上传小于20MB的图片");
            }
        }

        List<String> imagesUrl = ossUtils.uploadImages(images, ROOM_IMAGE.getUrl());
        return success(saveImagesToList(imagesUrl));
    }
}
